$(function(){
    var group_members_box = new Vue({
        el: "#group_apply",
        data: {
            token: '',
            keyword: '',
            apply_reason: '',
            apply_group_id: 0,

            datalist: [],           // 分页固定参数名
            page_num: 1,
            page_size: 25,
            total: 0,
            page_total: [],
            page_total_num: 0
        },
        mounted: function() {
            this.initialPage();
            this.getDataList();
        },
        watch: {// 数据变化事件
        },
        methods: {
            getLocalTime(shijianchuo) {
                var time = new Date(shijianchuo * 1000);
                var y = time.getFullYear();
                var m = time.getMonth()+1;
                var d = time.getDate();
                var h = time.getHours();
                var mm = time.getMinutes();
                var s = time.getSeconds();
                return y + '-' + (m) + '-' + (d) + ' ' + (h) + ':' + (mm)+':' + (s);
            },
            initialPage: function () {
                this.token = $('#lcm_lingpai').val();
                // 设置弹窗
                toastr.options = {
                    "closeButton": true, // 是否显示关闭按钮
                    "debug": false,       // 是否使用debug模式
                    "positionClass": "toast-top-center",// 弹出窗的位置
                    "showDuration": "100", // 显示的动画时间
                    "hideDuration": "100",// 消失的动画时间
                    "timeOut": "1000",     // 展现时间
                    "extendedTimeOut": "100",// 加长展示时间
                    "showEasing": "swing",    // 显示时的动画缓冲方式
                    "hideEasing": "linear",   // 消失时的动画缓冲方式
                    "showMethod": "fadeIn",   // 显示时的动画方式
                    "hideMethod": "fadeOut"   // 消失时的动画方式
                };
            },
            showApplyDialog: function(apply_group_id) {
                this.apply_group_id = apply_group_id;
                this.apply_reason = '';
                $('#modal-default').modal('show');
            },
            closeApplyDialog: function() {
                $('#modal-default').modal('hide');
            },
            getDataList: function () {
                $.ajax({
                    method: 'POST',
                    url: '/backend/group/listgrouppost',
                    data: {
                        _token: this.token,
                        keyword: this.keyword,
                        page_num: this.page_num,
                        page_size: this.page_size
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            group_members_box.datalist = res.data.list;
                            group_members_box.total = res.data.total;
                            $.each(group_members_box.datalist, function(key, val) {
                                val['create_time'] = group_members_box.getLocalTime(val['create_time']);
                            });

                            // 计算分页按钮
                            var total_page = (group_members_box.total/group_members_box.page_size).toFixed(0);
                            group_members_box.page_total_num = total_page;
                            var next_page = 0;
                            group_members_box.page_total = [group_members_box.page_num];
                            for (var i = 1; i <= 4; i++) {// 右边补入按钮
                                next_page = group_members_box.page_num + i;
                                if (next_page <= total_page) {
                                    group_members_box.page_total.push(next_page)
                                }
                            }

                            if (group_members_box.page_total.length < 5) {// 左边补入按钮
                                for (i = 1; i <= 4; i++) {              // 左边补入按钮
                                    next_page = group_members_box.page_num - i;
                                    if (group_members_box.page_total.length < 5 && next_page > 0) {
                                        group_members_box.page_total.unshift(next_page)
                                    }
                                }
                            }
                        } else {
                            toastr.error(res.message);
                        }
                    }
                })
            },
            addApply: function () {
                $.ajax({
                    method: 'POST',
                    url: '/backend/group/addapply',
                    data: {
                        _token: this.token,
                        apply_group_id: this.apply_group_id,
                        apply_reason: this.apply_reason
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            toastr.success(res.message);
                            group_members_box.getDataList();
                            group_members_box.closeApplyDialog();
                        }
                    }
                });
            },
            delGroupApply: function () {
                $.ajax({
                    method: 'POST',
                    url: '/backend/group/delapply',
                    data: {
                        _token: this.token,
                        apply_group_id: this.apply_group_id,
                        apply_reason: this.apply_reason
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            toastr.success(res.message);
                            group_members_box.getDataList();
                            group_members_box.closeApplyDialog();
                        }
                    }
                });
            }
        }
    });
});
